home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / uniq.pro < prev    next >
Encoding:
Text File  |  1997-07-08  |  2.1 KB  |  84 lines

  1. ; $Id: uniq.pro,v 1.4 1997/01/15 03:11:50 ali Exp $
  2. ;
  3. ; Copyright (c) 1988-1997, Research Systems, Inc.  All rights reserved.
  4. ;    Unauthorized reproduction prohibited.
  5.  
  6. ;+
  7. ; NAME:
  8. ;    UNIQ
  9. ;
  10. ; PURPOSE:
  11. ;    Return the subscripts of the unique elements in an array.
  12. ;
  13. ;    Note that repeated elements must be adjacent in order to be
  14. ;    found.  This routine is intended to be used with the SORT
  15. ;    function.  See the discussion of the IDX argument below.
  16. ;
  17. ;    This command is inspired by the Unix uniq(1) command.
  18. ;
  19. ; CATEGORY:
  20. ;    Array manipulation.
  21. ;
  22. ; CALLING SEQUENCE:
  23. ;    UNIQ(Array [, Idx])
  24. ;
  25. ; INPUTS:
  26. ;    Array:    The array to be scanned.  The type and number of dimensions
  27. ;        of the array are not important.  The array must be sorted
  28. ;        into monotonic order unless the optional parameter Idx is 
  29. ;        supplied.
  30. ;
  31. ; OPTIONAL INPUT PARAMETERS:
  32. ;    IDX:    This optional parameter is an array of indices into Array
  33. ;        that order the elements into monotonic order.
  34. ;        That is, the expression:
  35. ;
  36. ;            Array(Idx)
  37. ;
  38. ;        yields an array in which the elements of Array are
  39. ;        rearranged into monotonic order.  If the array is not
  40. ;        already in monotonic order, use the command:
  41. ;
  42. ;            UNIQ(Array, SORT(Array))
  43. ;
  44. ;        The expression below finds the unique elements of an unsorted
  45. ;        array:
  46. ;
  47. ;            Array(UNIQ(Array, SORT(Array)))
  48. ;
  49. ; OUTPUTS:
  50. ;    An array of indicies into ARRAY is returned.  The expression:
  51. ;
  52. ;        ARRAY(UNIQ(ARRAY))
  53. ;
  54. ;    will be a copy of the sorted Array with duplicate adjacent
  55. ;    elements removed.
  56. ;
  57. ; COMMON BLOCKS:
  58. ;    None.
  59. ;
  60. ; MODIFICATION HISTORY:
  61. ;    1988, AB, Written.
  62. ;    29 July 1992, ACY - Corrected for case of all elements the same.
  63. ;    Nov, 1995.  DMS, Return a 0 if argument is a scalar.
  64. ;
  65. ;-
  66. ;
  67.  
  68. function UNIQ, ARRAY, IDX
  69.  
  70. ; Check the arguments.
  71.   s = size(ARRAY)
  72.   if (s[0] eq 0) then return, 0        ;A scalar
  73.   if n_params() ge 2 then begin        ;IDX supplied?
  74.      q = array[idx]
  75.      indices = where(q ne shift(q,-1), count)
  76.      if (count GT 0) then return, idx[indices] $
  77.      else return, n_elements(q)-1
  78.   endif else begin
  79.      indices = where(array ne shift(array, -1), count)
  80.      if (count GT 0) then return, indices $
  81.      else return, n_elements(ARRAY)-1
  82.   endelse
  83. end
  84.